//
//  ClockSettingControllerStore.swift
//  Pomodoro
//
//  Created by zhengju on 2018/3/1.
//  Copyright © 2018年 zhengju. All rights reserved.
//

import Foundation
import UIKit
// MARK: - @protocol UITableViewDelegate
extension ClockSettingController: UITableViewDelegate {
    internal func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        switch indexPath.row {
        case 0:
            isAbsorbed = true
            pickerView.reloadAllComponents()
            return
        case 1:
            isAbsorbed = false
            pickerView.reloadAllComponents()
            return
        case 2:

            return
        case 3:
            
            return
        default:
            return
        }
        
        
        
    }

    internal func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
}

// MARK: - @protocol UITableViewDataSource
extension ClockSettingController: UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return leftTitles.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 44
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ClockSettingCell
        
        let title = leftTitles[indexPath.row]
        cell.leftLabel.text = title
        if indexPath.row == 0 {
            cell.rightLabel.text = absorbedTitle
        }else{
            cell.rightLabel.text = restTitle
        }
        
        return cell
        
    }
    
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return nil
    }
    
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return nil
    }
}


extension ClockSettingController:UIPickerViewDelegate{
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return self.isAbsorbed ? absorbedDatas[row] :restDatas[row]
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 35
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if isAbsorbed {
            absorbedTitle = absorbedDatas[row]
        }else{
            restTitle = restDatas[row]
        }
        tableView.reloadData()
    }
}
extension ClockSettingController:UIPickerViewDataSource{
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.isAbsorbed ? absorbedDatas.count :restDatas.count
    }
}
